import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.math.BigDecimal;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;



/*@Sangeeta
 *@Uses: td_idf calculation of bug reports. 
 * */
public class tf_idf 
{
   public Connection conn=null;
   public String userid="root";
   public String dbname="2jboss";
   public String passwd="123";   
   public ArrayList<word_frq> tf_idf_list= new ArrayList<word_frq>();
   ArrayList<docs> docs_coll = new ArrayList<docs>();
   	
public void process(Connection conn)
{
	Statement stmt=  null;
	int no_of_docs = 0;
	String str = "select summary,description from bug_svn_info";
	try
	{
		stmt =  conn.createStatement();
		stmt.execute(str);
		ResultSet rs = stmt.getResultSet();
		
		while(rs.next())
		{
			String title =rs.getString("summary");
			String desc = rs.getString("description");
			general_fun gf =  new general_fun();
			
			title  = gf.clean(title);
			title = gf.remove_stop_words(title);
			
			desc = gf.clean(desc);
			desc = gf.remove_stop_words(desc);
			
			String td  = title +" " + desc;
			
			String td_arr[] =td.split("\\s+");
			
			
			cal_tf(td_arr,tf_idf_list);//cal tf
			no_of_docs++;
		}
		
		System.out.println("No of docs ="+no_of_docs);
		
		//intr();
		
			
		if(rs!=null)
			rs.close();
		if(stmt!=null)
			stmt.close();
		
	} catch (SQLException e) 
	{	e.printStackTrace();}
	

	cal_idf(tf_idf_list,conn);
	cal_tf_idf(tf_idf_list,no_of_docs);
	insert_to_tf_idf(tf_idf_list,conn);
	
	   
}


private void cal_idf(ArrayList<word_frq> tf_idf_list, Connection conn)
{
	System.out.println("idf");
	
	Statement stmt =  null;
	String str = "select bugid,summary,description from bug_svn_info";
	try 
	{
		stmt =conn.createStatement();
		stmt.execute(str);
		ResultSet rs = stmt.getResultSet();
		while(rs.next())
		 {
			String bugid = rs.getString("bugid");
			String title = rs.getString("summary");
			String desc  =  rs.getString("description");
			String td = title+" "+desc;
			general_fun gf  = new general_fun();
			 td = gf.clean(td);
			 td = gf.remove_stop_words(td);
			 
			docs_coll.add(new docs(bugid,td)); 
			 
			
		 }//wh
	} catch (SQLException e)
	 {	e.printStackTrace(); }
	
	for(int i=0;i<tf_idf_list.size();i++)
	{
		System.out.println("idf="+i);
		String c_w =  tf_idf_list.get(i).word;
        
		for(int j=0;j<docs_coll.size();j++)
		{
		   String td_arr[]= docs_coll.get(j).td.split("\\s+");
		   
		   boolean present = false;
		   for(int k=0;k<td_arr.length;k++)
		   {
			   
			   if(td_arr[k].equalsIgnoreCase(c_w))
			   {
				   present =  true;
				   break;
				   
			   }//if
		   }//f
		   
		   if(present)
		   {
			   int idf = tf_idf_list.get(i).idf;
			   idf++;
			   word_frq   temp =  tf_idf_list.get(i);
			   temp.idf = idf;
			   tf_idf_list.set(i,temp );
		   }
		}//f
		
	}//f
	
}

private void cal_tf(String[] td_arr,ArrayList<word_frq> tf_idf_list)
{

  for(int i=0;i<td_arr.length;i++)
   {
	   String c_w = td_arr[i];
	   boolean present  = false;
	   int index = -1;
	   
	   for(int j=0;j<tf_idf_list.size();j++)
	   {
		   if(c_w.equalsIgnoreCase(tf_idf_list.get(j).word))
		   {
			   present =true;
			   index = j;
			   break;
		   }
	   }//for
	   
	   if(present)
	   {
		  int c_w_frq =  tf_idf_list.get(index).frq;
		  c_w_frq++;
		 
		  int idf_val = tf_idf_list.get(index).idf;
		
		  
		  word_frq  obj =  new word_frq(c_w,c_w_frq,idf_val,1);
		  tf_idf_list.set(index, obj);
		  
	   }
	   else
	   {
		   int c_w_frq = 1;
		   int idf_val = 0;
		   word_frq  obj =  new word_frq(c_w,c_w_frq,idf_val,1);
		   tf_idf_list.add(obj);
	   }
   }//for
  
}

private void cal_tf_idf(ArrayList<word_frq> tf_idf_list, int no_of_docs) 
{
	for(int i=0;i<tf_idf_list.size();i++)
	{
		//System.out.println("idf="+i);
		String c_w  =  tf_idf_list.get(i).word;
		int tf  = tf_idf_list.get(i).frq;
		int idf = tf_idf_list.get(i).idf;
		double tf_idf = (tf*1.0) *java.lang.Math.log(((no_of_docs*1.0)/idf));
		System.out.println("tf = "+tf+"  idf="+idf +"  tf_idf="+tf_idf);
		tf_idf_list.set(i, new word_frq(c_w,tf,idf,tf_idf)) ;
		//intr();
	}
	
}

	
private void insert_to_tf_idf(ArrayList<word_frq> tf_idf_list, Connection conn)
{
	Statement stmt =  null;
	
	for(int i=0;i<tf_idf_list.size();i++)
	{
		String word =tf_idf_list.get(i).word;
		int tf = tf_idf_list.get(i).frq;
		int idf = tf_idf_list.get(i).idf;
		double tf_idf = tf_idf_list.get(i).tf_idf;
		
		String str =  "insert into tf_idf values ('"+word+"',"+tf+","+idf+","+tf_idf+")";
		System.out.println("str="+str);
		try 
		 {
			stmt  = conn.createStatement();
			stmt.executeUpdate(str);
			
		} catch (SQLException e) 
		{ 	e.printStackTrace(); }
		
	}//for
	
	if(stmt!=null)
		try {
			stmt.close();
		} catch (SQLException e) {
		
			e.printStackTrace();
		}
	
}


public void intr()
 {
	 BufferedReader br =  new BufferedReader(new InputStreamReader(System.in));
	 System.out.println("INTR");
	 try {
		br.readLine();
	} catch (IOException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
	 
 }
 

public static void main(String args[])
{	
		  tf_idf obj =  new tf_idf();
		  db_conn db =  new db_conn();
		  obj.conn = db.connect(obj.dbname, obj.userid, obj.passwd);
		  obj.process(obj.conn);
		  db.close(obj.conn);
		  
	}//main
}
